package com.ewhale.feitengguest.presenter.user.mine;

import com.ewhale.feitengguest.api.ApiHelper;
import com.ewhale.feitengguest.dto.MyTaskDetailDto;
import com.ewhale.feitengguest.view.user.mine.MyTaskDetailView;
import com.ewhale.feitengguest.view.user.task.TaskDetailsView;
import com.simga.library.base.IPresenter;
import com.simga.library.http.APIException;
import com.simga.library.utils.CheckUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyTaskDetailPresenter extends IPresenter {


    public void loadMyTaskDetail(long missionId) {
        mView.showProLoading();
        request(1, ApiHelper.USER_API.loadMyTaskDetails(missionId), null);
    }

    public void commitMessage(long missionId, List<MyTaskDetailDto.ContentExplainDtosBean> contentlist, String imgUrls) {
        mView.showProLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("missionId", missionId);
        if (contentlist.size() > 0) {
            for (int i = 0; i < contentlist.size(); i++) {
                if (i == 0) {
                    params.put("content", contentlist.get(i).getContent());
                } else {
                    params.put("content" + (i + 1), contentlist.get(i).getContent());
                }
            }
        }
        if (!CheckUtil.isNull(imgUrls)) {
            params.put("imgUrls", imgUrls);
        }
        request(2, ApiHelper.USER_API.submitMission(params), null);

    }


    public void cancelTask(long missionId) {
        mView.showProLoading();
        request(3, ApiHelper.TASK_API.cancelTask(missionId), null);
    }


    @Override
    public <T> void onNext(int postCode, T result, Object data) {
        super.onNext(postCode, result, data);
        switch (postCode) {
            case 1:
                ((MyTaskDetailView) mView).showTaskDetails((MyTaskDetailDto) result);
                break;
            case 2:
                ((MyTaskDetailView) mView).commitSuccess();
                break;
            case 3:
                ((MyTaskDetailView) mView).cancelTaskSuccess();
                break;
        }
    }

    @Override
    public void otherException(int postCode, Object data, APIException e) {
        super.otherException(postCode, data, e);
        mView.showErrorMessage(postCode, e.getCode(), e.getDisplayMessage());
    }

}
